package com.hs.egova.controller;

import com.hs.egova.base.ResultInfo;
//import com.hs.egova.dao.JpaUserRepository;
import com.hs.egova.mapping.User;
import com.hs.egova.service.UserManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedList;
import java.util.List;

/**
 * @author husheng
 * @version v1.0
 * @date 2021/7/21 13:33
 * @email husheng@egova.com.cn
 */
@RestController
@RequestMapping("/jpa")
@Slf4j
@Api(tags = "jpa操作")
public class UserController {
   /* @Autowired
    private JpaUserRepository jpaUserRepository;*/

  /*  @Autowired
    private UserManager userManager*/

    private final UserManager userManager;

    public UserController(UserManager userManager) {
        this.userManager = userManager;
    }

    /**
     *
     * @param id
     * @return
     */
    @GetMapping("/getuser/{id}")
    @ApiOperation("根据id查找用户")
    public ResultInfo getUser(@PathVariable Long id) {
        log.info("id {}", id);
        ResultInfo resultInfo = userManager.getUserById(id);
        return resultInfo;
    }

    /**
     *
     * @param name
     * @return
     */
    @GetMapping("/getuserbyname/{name}")
    public ResultInfo getUserByName(@PathVariable String name) {
        log.info("name: {}", name);
        //User user = jpaUserRepository.findUserByUserName(name);
        return userManager.getUserByName(name);
    }

    /**
     *
     * @param name
     * @return
     */
    @GetMapping("/list/{name}")
    public List<User> listUser(@PathVariable String name) {
        //List<User> users = jpaUserRepository.findUsers(name);
        List<User> users = new LinkedList<>();
        return users;
    }

    @GetMapping("/v2/list/{username}")
    public ResultInfo listUserNew(@PathVariable String username) {
        return userManager.getUserByUserName(username);
    }

    @ApiOperation("根据姓名查询")
    @GetMapping("/v2/list1/{username}")
    public ResultInfo listUserNewTwo(@PathVariable String username) {
        return userManager.getUserByUserNameNew(username);
    }

    @ApiOperation("根据姓名和备注查找")
    @GetMapping("/v2/listuser")
    public ResultInfo listUser(String userName, String note) {
        return userManager.getUserByUserNameAndNote(userName, note);
    }

    @ApiOperation("分页查找")
    @GetMapping("/v2/page")
    public ResultInfo pageUser(
            @ApiParam(name="当前页", type = "Integer", example = "1", required = true) @RequestParam  int cur,
            @ApiParam(name = "每页数", type = "Integer", required = true) @RequestParam int num) {
        return userManager.page(cur, num);
    }
    /**
     * 有注解@RequestParam
     * @param id
     * @param userName
     * @return
     */
    @PostMapping("/v3/test1")
    public User testOne(@RequestParam("id") Long id, @RequestParam("userName") String userName) {
        User user = User.builder()
                .id(id)
                .userName(userName)
                .note("测试")
                .build();
        return user;
    }

    /**
     * 无RequestParam 注解
     * @param id
     * @param userName
     * @return
     */
    @PostMapping("/v3/test2")
    public User testTwo(Long id, String userName) {
        User user = User.builder()
                .id(id)
                .userName(userName)
                .note("测试无RequestParam")
                .build();
        return user;
    }

    /**
     *  前端请求类型 id=2&userName=hu
     * @param user
     * @return
     */
    @ApiOperation("用户信息获取")
    @PostMapping("/v3/test3")
    public ResultInfo testThree(User user) {
        user.setNote("无注解，用对象接收");
        ResultInfo resultInfo = new ResultInfo(true);
        resultInfo.setData("user", user);
        return resultInfo;
    }

    @PostMapping("/v3/test4")
    public User testFour(@RequestBody User user) {
        user.setNote("RequestBody注解");
        return user;
    }
}
